package com.imay.live.activity.family.controller;

import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.imay.live.R;
import com.imay.live.activity.family.util.LevelUtil;
import com.imay.live.adapter.family.FamilyUserAdapter;
import com.imay.live.adapter.family.FamilyUserLevelAdapter;
import com.imay.live.manager.FamilyAccountManager;
import com.imay.live.model.UserExpsBean;
import com.imay.live.utils.RecyclerViewUtil;
import com.imay.live.utils.ViewUtil;
import com.imay.live.view.indicator.slidebar.ScrollBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/31 0031.
 */

public class FamilyPersonLevelController extends BaseFamilyController {

    private RecyclerView rl_family_person_level;
    FamilyUserLevelAdapter adapter;

    @Override
    protected void initView() {
        rl_family_person_level = ViewUtil.findView(activity, R.id.rl_family_person_level);
        RecyclerViewUtil.setLinearLayoutManager(activity, rl_family_person_level);

        List<MultiItemEntity> userExps = new ArrayList<>();
        Map<Integer, UserExpsBean> map = FamilyAccountManager.getInstance().getFamilyUserExps();
        for (Integer key : map.keySet()) {
            userExps.add(map.get(key));
        }
        Collections.sort(userExps, new Comparator<MultiItemEntity>() {
            @Override
            public int compare(MultiItemEntity o1, MultiItemEntity o2) {
                if (o1.getItemType() == FamilyUserLevelAdapter.VIEWTYPE_LINEAR_CONTENT
                        && o2.getItemType() == FamilyUserLevelAdapter.VIEWTYPE_LINEAR_CONTENT) {
                    UserExpsBean userExpsBean1 = (UserExpsBean) o1;
                    UserExpsBean userExpsBean2 = (UserExpsBean) o2;
                    return userExpsBean1.getLevel() - userExpsBean2.getLevel();
                }
                return 0;
            }
        });
        adapter = new FamilyUserLevelAdapter(userExps);

        rl_family_person_level.setAdapter(adapter);

        initFootView();
    }

    public void initFootView() {
        View view = LayoutInflater.from(activity).inflate(R.layout.view_family_level_part1, null);
        adapter.addFooterView(view);
    }

    public void initHeadView(int level) {
        TextView me_level = new TextView(activity);
        me_level.setTextColor(activity.getResources().getColor(LevelUtil.getLevelColor(level)));
        me_level.setTextSize(24);
        me_level.setText(FamilyAccountManager.getLevelText(level));
        me_level.setGravity(Gravity.CENTER);
        RecyclerView.LayoutParams params = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, (int) activity.getResources().getDimension(R.dimen.size_84dp));
        me_level.setLayoutParams(params);

        adapter.addHeaderView(me_level);
        adapter.notifyItemInserted(0);
    }
}
